<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('课题金额修改')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: select2-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-apply-edit" th:object="${sciHorizontalApply}">
            <input name="id" id="ktid" th:field="*{id}" type="hidden">
            <input name="reid" id="reid" th:field="*{reid}" type="hidden">
            <input name="state" id="state" th:field="*{state}" type="hidden">
<!--            <input name="amountType" id="amountType" type="hidden" value="立项">-->
            <h4 class="form-header h4">基本信息</h4>
            <div class="row">
            <div class="col-xs-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">申请人：</label>
                    <div class="col-sm-8">
                        <input  readonly th:field="*{userName}" class="form-control" type="text" >
                    </div>
                </div>
            </div>
            <div class="col-xs-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">课题编号：</label>
                    <div class="col-sm-8">
                        <input  readonly th:field="*{topNumber}" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-xs-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">课题类型：</label>
                    <div class="col-sm-8">
                        <select name="topType" class="form-control" th:with="type=${@dict.getType('sci_top_type')}" required disabled>
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{topType}"></option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-xs-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">课题名称：</label>
                    <div class="col-sm-8">
                        <input name="topName" th:field="*{topName}" class="form-control" type="text" required readonly>
                    </div>
                </div>
            </div>
            <div class="col-xs-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">项目金额：</label>
                    <div class="col-sm-8">
                        <input name="amount" th:field="*{amount}" class="form-control" type="number" required readonly>
<!--                            <span class="input-group-addon">-->
<!--                                <a href="#" title="上传到账金额"  data-toggle="modal" data-target="#myModal"><i class="fa fa-plus-circle" ></i>到账金额</a>-->
<!--                            </span>-->
                    </div>
                </div>
            </div>
            <div class="col-xs-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">申请日期：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <input name="signingData" autocomplete="off" th:field="*{signingData}" th:value="${#dates.format(signingData, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text" required disabled>
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-xs-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">到账金额：</label>
                    <div class="col-sm-8">
                        <input id="reamount" th:field="*{reAmount}" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">到账日期：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <input name="accountData" autocomplete="off" th:field="*{accountData}" th:value="${#dates.format(signingData, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text" required >
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-xs-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">主持人：</label>
                    <div class="col-sm-8">
<!--                        <input name="firstPersonId" th:field="*{firstPersonId}" class="form-control" type="text" required>-->
                        <select name="firstPersonId" class="form-control m-b" required disabled>
                            <option value="">无</option>
                            <option th:each="user : ${sysUsers1}" th:text="${user.userName}" th:value="${user.userId}" th:selected="${ #strings.toString(user.userId) == sciHorizontalApply.firstPersonId}"></option>
                        </select>
                    </div>
                </div>
            </div>

            <div class="col-xs-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">成员1：</label>
                    <div class="col-sm-8">
<!--                        <input name="secondPersonTask" th:field="*{secondPersonTask}" class="form-control" type="text" required>-->
                        <select name="secondPersonId" class="form-control m-b" required disabled>
                            <option value="">无</option>
                            <option th:each="user : ${sysUsers1}" th:text="${user.userName}" th:value="${user.userId}" th:selected="${ #strings.toString(user.userId) == sciHorizontalApply.secondPersonId}" ></option>
                        </select>
                    </div>
                </div>
            </div>

            <div class="col-xs-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">成员2：</label>
                    <div class="col-sm-8">
<!--                        <input name="thirdPersonId" th:field="*{thirdPersonId}" class="form-control" type="text" required>-->
                        <select name="thirdPersonId" class="form-control m-b" required disabled>
                            <option value="">无</option>
                            <option th:each="user : ${sysUsers1}" th:text="${user.userName}" th:value="${user.userId}" th:selected="${ #strings.toString(user.userId) == sciHorizontalApply.thirdPersonId}"  ></option>
                        </select>
                    </div>
                </div>
            </div>

            <div class="col-xs-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">成员3：</label>
                    <div class="col-sm-8">
<!--                        <input name="fourthPersonId" th:field="*{fourthPersonId}" class="form-control" type="text" required>-->
                        <select name="fourthPersonId" class="form-control m-b" required disabled>
                            <option value="">无</option>
                            <option th:each="user : ${sysUsers1}" th:text="${user.userName}" th:value="${user.userId}" th:selected="${ #strings.toString(user.userId) == sciHorizontalApply.fourthPersonId}"  ></option>
                        </select>
                    </div>
                </div>
            </div>
            <!-- 其他成员（第5位及以后）：只读展示 -->
            <div class="col-xs-12" th:if="${extraMembers != null}">
                <div class="form-group">
                    <label class="col-sm-2 control-label">其他成员：</label>
                    <div class="col-sm-10">
                        <div id="memberContainer" class="row">
                            <div class="col-xs-3 m-b-xs" th:each="mid : ${extraMembers}">
                                <select class="form-control m-b" disabled>
                                    <option th:each="user : ${sysUsers1}" th:text="${user.userName}" th:value="${user.userId}" th:selected="${ #strings.toString(user.userId) == mid }"></option>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-xs-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">佐证材料：</label>
                    <div class="col-sm-8">
                        <input type="hidden" name="reamountUrl" th:field="*{reamountUrl}">
                       <div class="file-loading">
                            <input class="form-control file-upload" id="reamountUrl" name="file" type="file">
                        </div>
                    </div>
                </div>
            </div>
            </div>

            <h4 class="form-header h4"> 批阅记录</h4>
            <div class="row">
                <div class="col-sm-12">
                    <div class="form-group">
                        <table id="bootstrap-table"></table>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <div class="row">
        <div class="col-sm-offset-5 col-sm-10">
            <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()">    保 存</button>&nbsp;
            <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"> 关 闭 </button>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <th:block th:include="include :: select2-js" />
    <script type="text/javascript">

        $(function(){
            <!-- laydate示例 -->
            layui.use('laydate', function(){
                var laydate = layui.laydate;
                laydate.render({
                    elem: '#laydate-demo-4',
                    range: true
                });
                var startDate = laydate.render({
                    elem: '#laydate-startTime',
                    max: $('#laydate-endTime').val(),
                    theme: 'molv',
                    trigger: 'click',
                    done: function(value, date) {
                        // 结束时间大于开始时间
                        if (value !== '') {
                            endDate.config.min.year = date.year;
                            endDate.config.min.month = date.month - 1;
                            endDate.config.min.date = date.date;
                        } else {
                            endDate.config.min.year = '';
                            endDate.config.min.month = '';
                            endDate.config.min.date = '';
                        }
                    }
                });
                var endDate = laydate.render({
                    elem: '#laydate-endTime',
                    min: $('#laydate-startTime').val(),
                    theme: 'molv',
                    trigger: 'click',
                    done: function(value, date) {
                        // 开始时间小于结束时间
                        if (value !== '') {
                            startDate.config.max.year = date.year;
                            startDate.config.max.month = date.month - 1;
                            startDate.config.max.date = date.date;
                        } else {
                            startDate.config.max.year = '';
                            startDate.config.max.month = '';
                            startDate.config.max.date = '';
                        }
                    }
                });
            });

            var kid = $("#ktid").val();
            var options = {
                url: prefix + "/bhyy/"+kid,
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                pagination:false,

                columns: [

                    {
                        field : 'createTime',
                        title : '批阅时间',
                        width: 200

                    } ,
                    {
                        field : 'uname',
                        title : '批阅人',
                        width: 100
                    },
                    {
                        field : 'concate',
                        title : '内容',
                        width: 500
                     },
                    {
                        field : 'state',
                        title : '审核状态',
                        width: 100
                    }
                ]
            };
            $.table.init(options);
        });
    </script>
    <script th:inline="javascript">
        var prefix = ctx + "system/apply";
        $("#form-apply-edit").validate({
            focusCleanup: true
        });

        $("input[name='reamount']").on('input', function() {
            var value = $(this).val();
            // 限制整数部分不超过9位
            if (value) {
                var numericValue = parseFloat(value);
                // 限制整数部分长度
                if (value.indexOf('.') !== -1) {
                    var parts = value.split('.');
                    if (parts[0].length > 9) {
                        $(this).val(parts[0].substring(0, 9) + (parts[1] ? '.' + parts[1] : ''));
                    }
                } else {
                    if (value.length > 9) {
                        $(this).val(value.substring(0, 9));
                    }
                }
                // 限制小数点后只能输入四位
                if (value.indexOf('.') !== -1) {
                    var parts = value.split('.');
                    if (parts[1] && parts[1].length > 4) {
                        $(this).val(parts[0] + '.' + parts[1].substring(0, 4));
                    }
                }
                // 限制最大值
                var valueStr = numericValue.toString();
                if (valueStr.length > 9){
                    this.value = "";
                    $.modal.alertError("输入金额有误差：总长度最大9位数，小数点后最大四位；例：9999.9999")
                }
            }
        });

        function submitHandler() {
            if ($.validate.form()) {
                // 校验项目金额不能为负数
                var projectAmount = $("input[name='reamount']").val();
                if (parseFloat(projectAmount) <= 0) {
                    return $.modal.alertError("请输入有效的项目金额");
                }

                // 校验项目金额小数点后最多4位
                if (!/^\d+(\.\d{1,4})?$/.test(projectAmount)) {
                    return $.modal.alertError("项目金额小数点后最多4位");
                }
                $.operate.saveTab(prefix + "/reamountedit", $('#form-apply-edit').serialize());
            }
        }

        $("input[name='signingData']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='accountData']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });



        $(".file-upload").each(function (i) {
            var val = $("input[name='" + this.id + "']").val();
            var initialPreview = [];
            var initialPreviewConfig = [];
            
            // 如果有已上传的文件，配置预览
            if (val && val.trim() !== '') {
                initialPreview = [val];
                initialPreviewConfig = [{
                    type: "pdf",
                    caption: val.split('/').pop(),
                    url: ctx + "common/delete",
                    key: val
                }];
            }
            
            $(this).fileinput({
                'uploadUrl': ctx + 'common/upload/hxkt',
                initialPreviewAsData: true,
                initialPreview: initialPreview,
                initialPreviewConfig: initialPreviewConfig,
                maxFileCount: 1,
        maxFileSize: 51200,
                autoReplace: true,
                overwriteInitial: true,
                allowedFileExtensions: ["pdf"],
                deleteUrl: ctx + "common/delete",
                slugCallback: function(filename) {
                    if ($("input[name='topName']").val()===""){
                        $(this).fileinput('clear');
                        $.modal.alertError("请先输入课题名称");
                        return false;
                    }
                    // 自定义文件名生成规则
                    fileExtension = filename.split('.').pop();
                    const year = new Date().getFullYear();
                    const newName = year +"-"+ $("input[name='topName']").val() +"-"+ "金额佐证材料"+ "." + fileExtension;
                    return newName;
                },
            }).on('fileuploaded', function (event, data, previewId, index) {
                if (data.response && data.response.url) {
                    $("input[name='" + event.currentTarget.id + "']").val(data.response.url);
                }
            }).on('fileremoved', function (event, id, index) {
                $("input[name='" + event.currentTarget.id + "']").val('');
            }).on('fileclear', function (event) {
                $("input[name='" + event.currentTarget.id + "']").val('');
            });
        });
    </script>
</body>
</html>